# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python
from cerbero.utils import shell

class Recipe(recipe.Recipe):
    name = 'libmpeg2'
    version = '0.5.1'
    stype = SourceType.TARBALL
    url = 'http://libmpeg2.sourceforge.net/files/libmpeg2-0.5.1.tar.gz'
    licenses = [License.GPLv2Plus]
    autoreconf = True
    patches = ['libmpeg2/0001-Disable-AC_C_ALWAYS_INLINE.patch',
               'libmpeg2/0002-Import-revision-1206-from-upstream-to-fix-PIE-build.patch']

    files_libs = ['libmpeg2', 'libmpeg2convert']
    files_bins = ['mpeg2dec', 'extract_mpeg2', 'corrupt_mpeg2']
    files_devel = ['include/mpeg2dec', 'lib/pkgconfig/libmpeg2.pc',
                   'lib/pkgconfig/libmpeg2convert.pc']

    def prepare(self):
        if self.config.variants.nox11:
            self.configure_options += ' --without-x '
        if self.config.variants.nosdl:
            self.configure_options += ' --disable-sdl '
        if self.config.target_platform == Platform.IOS:
            if Architecture.is_arm(self.config.target_arch):
                if 'GAS' in os.environ:
                    self.new_env = {'CCAS': os.environ['GAS']}
                    self.new_env['CCAS'] += ' -no-integrated-as '
                self.append_env = {'LDFLAGS': ' -Wl,-read_only_relocs,suppress'}

    def compile(self):
        if self.config.target_platform == Platform.IOS:
            if Architecture.is_arm(self.config.target_arch):
                arm_f = os.path.join(self.build_dir, 'libmpeg2', 'motion_comp_arm_s.S')
                shell.replace(arm_f,
                    {'.global MC_put_o_16_arm\nMC_put_o_16_arm:': '.global _MC_put_o_16_arm\n_MC_put_o_16_arm:',
                     '.global MC_put_o_8_arm\nMC_put_o_8_arm:': '.global _MC_put_o_8_arm\n_MC_put_o_8_arm:',
                     '.global MC_put_x_16_arm\nMC_put_x_16_arm:': '.global _MC_put_x_16_arm\n_MC_put_x_16_arm:',
                     '.global MC_put_x_8_arm\nMC_put_x_8_arm:': '.global _MC_put_x_8_arm\n_MC_put_x_8_arm:',
                    })
        super(recipe.Recipe, self).compile()
